package com.kyzh.core.adapters

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.PagerAdapter
import com.gushenge.core.beans.ShareHistory
import com.gushenge.core.requests.WealRequest
import com.kyzh.core.R
import com.kyzh.core.dialog.showEditDialog
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import org.jetbrains.anko.toast


class ShareHistoryTabAdapter(val context: Context, val titles: ArrayList<String>) : PagerAdapter() {

    var p1=1
    var max1 = 1

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val view = View.inflate(context, R.layout.recyclerview,null)
        val root = view.findViewById<SmartRefreshLayout>(R.id.root)
        val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView)

        val beans = arrayListOf<ShareHistory>()
        recyclerView.layoutManager = object :LinearLayoutManager(context){
            override fun canScrollHorizontally(): Boolean {
                return false
            }
        }
        p1=1
        max1=1
        val adapter = ShareHistoryAdapter(R.layout.item_share_history, beans,position)
        recyclerView.adapter = adapter
        root.autoRefresh()
        root.setEnableAutoLoadMore(false)
        root.setOnRefreshListener {
            root.finishRefresh(1000)
            p1=1
            addData(root,beans,adapter,position)
        }
//        adapter.setOnItemClickListener{a,b,c->
//            val shareHistory = a.data[c] as ShareHistory
//            if(!shareHistory.is_fafang ){
//                context.showEditDialog("发放奖励"){
//                    if(this.isNotEmpty()){
//                        WealRequest.sendShareGift(shareHistory.user_id,this){
//                            addData(root,beans,adapter,position)
//                        }
//                    }
//                }
//            }
//        }
        root.setOnLoadMoreListener {
            root.finishLoadMore(1000)
            if (p1>max1){
                context.toast(context.getString(R.string.noHaveMore))
            }else{
                addData(root,beans,adapter,position)
            }
        }

        container.addView(view)
        return view
    }

    private fun addData(root: SmartRefreshLayout, bean: ArrayList<ShareHistory>, adapter: ShareHistoryAdapter, position: Int) {
            WealRequest.shareHistory(position, p1){ p , max , beans ->
                root.finishRefresh()
                root.finishLoadMore()
                if (p == 2) {
                    bean.clear()
                }
                bean.addAll(beans)
                adapter.notifyDataSetChanged()
                p1 = p
                max1 = max
            }
    }


    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view ==`object`
    }

    override fun getCount(): Int {
        return titles.size
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View)
    }

    override fun getPageTitle(position: Int): CharSequence {
        return titles[position]
    }

    fun getTabView(position: Int): View {
        val view: View = LayoutInflater.from(context).inflate(R.layout.sharehistory_tab, null)
        val textView = view.findViewById<TextView>(R.id.root)
        textView.text = titles[position]
        return view
    }
}